﻿/*
    统一创建静态实体并给定默认值，方便以后统一初始化实体时候使用
 */
using Abp.Authorization.Users;
using SOEI.Solution.Authorization.Users;
using SOEI.Solution.Customized;
using SOEI.Solution.DomainEntities.Parameters;
using SOEI.Solution.Integratedes.ElectronicSignet;
using SOEI.Solution.Integratedes.Organization;
using SOEI.Solution.ThirdParty.ESignClient.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SOEI.Solution.Helper
{
    public static class EntityHelper
    {
        /// <summary>
        /// 获取新的经济机构实体 OEconomyOrgani 不包含序列主键
        /// </summary>
        /// <returns></returns>
        public static OEconomyOrgani GetNewOEconomyOrgani()
        {
            var entity = new OEconomyOrgani()
            {
                JGMC = "新注册的经济机构",
                // 买卖合同签订编号
                KZJQDMMHTBH = "02",
                // 经纪合同效验编号
                JJHTJYBH = "02",
                // 校验挂牌信息编号
                JYGPXXBH = "01",
                // 可网签
                KWQ = "0",
                // 是否个体工商户
                SFGTGSH = string.Empty,
                // 是否网签  
                SFYQ = string.Empty,
                // 设置机构状态为待提交
                JGZT = ((int)EApprovalStatus.WaitSubmit).ToString()
            };

            return entity;
        }

        /// <summary>
        /// 获取新的经济机构管理员实体（Zhxt）不包含序列主键
        /// </summary>
        /// <param name="entity">所属的经济机构实体</param>
        /// <param name="plainPassword">默认密码</param>
        /// <returns></returns>
        public static OEconomyAdmin GetNewOEconomyAdmin(OEconomyOrgani entity, string plainPassword)
        {
            // 创建一个管理员
            var entityEconomyAdmin = new OEconomyAdmin
            {
                JGBH = entity.Id,
                RYZT = ((int)EApprovalStatus.Reviewed).ToString(), //已审核
                MM = plainPassword,
                RYLX = "0",
                XB = string.Empty,
                KWQ = "1",
                RYMC = entity.JGMC + "的管理员",

            };
            return entityEconomyAdmin;
        }

        /// <summary>
        ///  根据zhxt人员信息创建一个本地系统管理员用户实体
        /// </summary>
        /// <param name="OEconomyPerson"></param>
        /// <returns></returns>
        public static User GetNewUserPerson(OEconomyPerson OEconomyPerson)
        {
            // 2. 创建新的用户
            var entity = new User
            {
                TenantId = null,
                PhoneNumber = OEconomyPerson.LXDH,
                IsPhoneNumberConfirmed = false, // 是否验证过电话号码
                IsActive = true,
                Roles = new List<UserRole>(),
                /*OrganizeID = orgEntity.Id,   // 机构编号*/
                UserType = EUserType.Person, // 这个用户为机构管理员
                OriginalPassword = OEconomyPerson.MM,      // 原始明文密码，老系统需要用到
                UserName = OEconomyPerson.Id, // 登录名称就是电话号码(登录账号就是原始用户表序列)
                Name = OEconomyPerson.RYMC,
                Surname = "管理员",
                EmailAddress = string.Format("{0}@example.com", OEconomyPerson.Id), // 生成一个账号开始的邮箱
                IsEmailConfirmed = false,   // 邮箱没有验证
                OriginalNumber = OEconomyPerson.JGBH // 对应zhxt中的经济机构编号
            };
            entity.SetNormalizedNames();

            return entity;
        }

        /// <summary>
        /// 返回一个新的电子签章实体
        /// </summary>
        /// <returns></returns>
        public static OrgSignet GetNewOrgSignet()
        {
            var entity = new OrgSignet()
            {
                SignetModuleID = Guid.NewGuid(),
                Status = ((int)EApprovalStatus.WaitSubmit).ToString()
            };

            return entity;
        }

        /// <summary>
        /// 返回一个新的承诺书
        /// </summary>
        /// <returns></returns>

        public static PledgeBook GetNewPledgeBook()
        {
            var entity = new PledgeBook()
            {
                PledgeBookModuleID = Guid.NewGuid(),
                Status = ((int)EApprovalStatus.WaitSubmit).ToString()
            };

            return entity;
        }

        /// <summary>
        /// 机构实体转换为E签宝第三方账号
        /// </summary>
        /// <param name="Organi"></param>
        /// <returns></returns>
        public static AccountEntity ToAccountEntity(this OEconomyOrgani Organi)
        {
            return new AccountEntity
            {
                ThirdPartyUserId = Organi.GLYBH,
                IdNumber = Organi.FRZJHM,
                Name = Organi.FRXM,
                Mobile = Organi.FRDH
            };
        }

        /// <summary>
        /// 机构实体转换为E签宝第三方机构账号
        /// </summary>
        /// <param name="Organi"></param>
        /// <returns></returns>
        public static OrgAccountEntity ToOrgniAccountEntity(this OEconomyOrgani Organi,OrgSignet OrgSignet)
        {
            return new OrgAccountEntity
            {
                ThirdPartyUserId = "Q" + Organi.Id, // 为了防止第三方机构账号和和人员编号重复，机构账号以Q开头
                Creator = OrgSignet.AccountID,
                   Name = Organi.JGMC,
                IdNumber = Organi.FRDMZH, // 组织机构代码证
                OrgLegalName = Organi.FRXM,
                OrgLegalIdNumber= Organi.FRZJHM
            };
        }

        /// <summary>
        /// 机构人员实体转换为E签宝第三方账号
        /// </summary>
        /// <param name="Person"></param>
        /// <returns></returns>
        public static AccountEntity ToAccountEntity(this OEconomyPerson Person)
        {
            return new AccountEntity
            {
                ThirdPartyUserId = Person.Id,
                IdNumber = Person.SFZH,
                Name = Person.RYMC,
                Mobile = Person.LXDH
            };
        }

        /// <summary>
        /// 返回指定的实体参数对象，在集合中查找
        /// </summary>
        /// <param name="Parameters"></param>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static Parameter GetEntityParameter(IList<Parameter> Parameters, string Key)
        {
            if (Key == null) return new Parameter { Id = string.Empty, Value = string.Empty };

            var entity = Parameters.DefaultIfEmpty(new Parameter { Id = string.Empty, Value = string.Empty }).FirstOrDefault(x => x.Id == Key);

            if (entity.Value == null) {
                entity.Value = string.Empty;
            }
            return entity;
        }
    }
}
